//
//  YTHDHotCommentController.m
//  yitaicaijing
//
//  Created by DIANGHONG on 2018/7/30.
//  Copyright © 2018年 DIANGHONG. All rights reserved.
//

#import "YTHDHotCommentController.h"
#import "YTHDCommentCell.h"

NSString *hotCommentCellID = @"hotCommentCellID";
@interface YTHDHotCommentController ()
@property(nonatomic ,strong)YTDHNewComment *model;
@end

@implementation YTHDHotCommentController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setUpTableView];
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage dh_setNavImageName:@"倛袨1"]
                                                                             style:UIBarButtonItemStylePlain
                                                                            target:self
                                                                            action:@selector(backButtonClick)];
    
    self.navigationItem.title = @"热门评论";
}

- (void)backButtonClick
{
    [self.navigationController popViewControllerAnimated:true];
}



// 设置tableview
- (void)setUpTableView
{
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self loadDataWithPullup:NO];
    }];
    
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        [self loadDataWithPullup:YES];
    }];
    
    header.lastUpdatedTimeLabel.hidden = YES;
    [footer setTitle:@"没有更多了哦~" forState:MJRefreshStateNoMoreData];
    [footer setTitle:@"" forState:MJRefreshStateIdle];
    
    self.tableView.mj_header = header;
    self.tableView.mj_footer = footer;
    
    
    self.tableView.rowHeight = 80;
    
    self.tableView.tableFooterView = [[UIView alloc] init];
    
    // 注册可重用cell
    [self.tableView registerNib:[UINib nibWithNibName:@"YTHDCommentCell" bundle:nil]
                                forCellReuseIdentifier:hotCommentCellID];
    
    [self.tableView.mj_header beginRefreshing];
}

#pragma mark: - 加载数据
- (void)loadDataWithPullup:(BOOL)pullUp
{
    int page = self.model.page ? self.model.page.intValue : 1;
    if (pullUp) {
        page++;
        if (page > self.model.total.intValue) {
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
            return;
        }
    }else{
        page = 1;
    }
    
    // 发送网络请求
    [YTNetworkManager.shareManager requestHotCommentWithTid:self.ID
                                                      paged:page
                                                  page_size:@"10"
                                                 completion:^(YTDHNewComment *comModel, BOOL isSucc, NSString *error)
    {
        if (!isSucc) {
            [self.view showTipWithTipStr:error];
            pullUp ? [self.tableView.mj_footer endRefreshing] : [self.tableView.mj_header endRefreshing];
            return ;
        }
        
        // 成功
        if (!pullUp) {
            self.model = comModel;
            [self.tableView.mj_header endRefreshing];
        }else{
            self.model.list = [self.model.list arrayByAddingObjectsFromArray:comModel.list];
            [self.tableView.mj_footer endRefreshing];

        }
        [self.tableView reloadData];
    }];
}




#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.model.list.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    YTHDCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:hotCommentCellID forIndexPath:indexPath];
    
    cell.commentModel = self.model.list[indexPath.row];
    
    return cell;
    
}



@end

